import { sys } from "cc";
import Singleton from "../Base/Singleton";
import { Event_Enum, Item_Status_Enum, Item_Type_Enum, SCENE_ENUM, Trigger_Status_Enum } from "../Enum";
import EventManager from "./EventManager";

export interface IItem {
    status: Item_Status_Enum,
    type: Item_Type_Enum,
}

const STORAGE_KEY = 'STORAGE_KEY';

export default class DataManager extends Singleton {

    static get Instance() {
        return super.getInstance<DataManager>();
    }

    private _items: Array<IItem> = [
        { status: Item_Status_Enum.Scene, type: Item_Type_Enum.Key },
        { status: Item_Status_Enum.Disabled, type: Item_Type_Enum.Mail },
    ];
    readonly _H2AAnswer: number[] = [0, 1, 2, 3, 4, 5, null];
    readonly _H2AInitData: number[] = [1, 0, 3, 2, 5, 4, null];
    private _H2AData: number[] = [...this._H2AInitData];

    private _curItemType: Item_Type_Enum | null = null;
    private _isSelect: boolean = false;
    private _mailBoxStatus: Trigger_Status_Enum = Trigger_Status_Enum.Pendding;
    private _grandmaStatus: Trigger_Status_Enum = Trigger_Status_Enum.Pendding;
    private _doorStatus: Trigger_Status_Enum = Trigger_Status_Enum.Pendding;
    private _curScene: SCENE_ENUM = SCENE_ENUM.H1;
    private _grandmaDialogIndex: number = -1;

    get curScene() {
        return this._curScene;
    }
    set curScene(newData: SCENE_ENUM) {
        this._curScene = newData;

        this.render();
    }
    get doorStatus() {
        return this._doorStatus;
    }
    set doorStatus(newData: Trigger_Status_Enum) {
        this._doorStatus = newData;

        this.render();
    }

    get H2AData() {
        return this._H2AData;
    }
    set H2AData(newData: number[]) {
        this._H2AData = newData;

        this.render();
    }

    get grandmaDialogIndex() {
        return this._grandmaDialogIndex;
    }
    set grandmaDialogIndex(newStatus: number) {
        this._grandmaDialogIndex = newStatus;

        this.render();
    }

    get grandmaStatus() {
        return this._grandmaStatus;
    }
    set grandmaStatus(newStatus: Trigger_Status_Enum) {
        this._grandmaStatus = newStatus;

        this.render();
    }

    get mailBoxStatus() {
        return this._mailBoxStatus;
    }
    set mailBoxStatus(newStatus: Trigger_Status_Enum) {
        this._mailBoxStatus = newStatus;

        this.render();
    }

    get isSelect() {
        return this._isSelect;
    }
    set isSelect(newStatus: boolean) {
        this._isSelect = newStatus;

        this.render();
    }

    get curItemType() {
        return this._curItemType;
    }

    set curItemType(newType: Item_Type_Enum | null) {
        this._curItemType = newType;

        this.render();
    }

    get items() {
        return this._items;
    }

    set items(newItems: IItem[]) {
        this._items = newItems;

        this.render();
    }

    render() {
        EventManager.Instance.emit(Event_Enum.Render);
        this.saveStorage();
    }

    saveStorage() {
        sys.localStorage.setItem(STORAGE_KEY, JSON.stringify({
            items: this._items,
            H2AData: this._H2AData,
            curItemType: this._curItemType,
            isSelect: this._isSelect,
            mailBoxStatus: this._mailBoxStatus,
            grandmaStatus: this._grandmaStatus,
            doorStatus: this._doorStatus,
            curScene: this._curScene,
            grandmaDialogIndex: this._grandmaDialogIndex,
        }));
    }

    restore() {
        const data = sys.localStorage.getItem(STORAGE_KEY);
        try {
            const parsedData = JSON.parse(data);
            this.items = parsedData.items;
            this.H2AData = parsedData.H2AData;
            this.curItemType = parsedData.curItemType || null;
            this.isSelect = parsedData.isSelect;
            this.mailBoxStatus = parsedData.mailBoxStatus;
            this.grandmaStatus = parsedData.grandmaStatus;
            this.doorStatus = parsedData.doorStatus;
            this.curScene = parsedData.curScene;
            this.grandmaDialogIndex = parsedData.grandmaDialogIndex;
        } catch (e) {
            this.reset();
        }
    }
    reset() {
        this.items = [
            { status: Item_Status_Enum.Scene, type: Item_Type_Enum.Key },
            { status: Item_Status_Enum.Disabled, type: Item_Type_Enum.Mail },
        ];
        this.H2AData = [...this._H2AInitData];

        this.curItemType = null;
        this.isSelect = false;
        this.mailBoxStatus = Trigger_Status_Enum.Pendding;
        this.grandmaStatus = Trigger_Status_Enum.Pendding;
        this.doorStatus = Trigger_Status_Enum.Pendding;
        this.curScene = SCENE_ENUM.H1;
        this.grandmaDialogIndex = -1;
    }
}
